<?php /* УТФ-8:fix */

class SPImodel__media__manage extends SPImodel__media
{
    public function insert( $gid, $pid, $description, $media_attrs )
    {
        $pid = (int) $pid;

        //---------------------------------------------------------------------
        // Detect next order_index value

        $query =    " SELECT MAX(order_index) AS max_order_index "
               .      " FROM ".SPIcfg::$store['table:media']
               .     " WHERE gid='" . addslashes( $gid ) . "' AND " . ( $pid == 0 ? " pid IS NULL " : " pid={$pid} " )
               ;

        $max_order_index = SPIstore::I()->query( $query )->fetch_and_close();

        $max_order_index = $max_order_index === false ? 10 : $max_order_index['max_order_index']+10;

        //---------------------------------------------------------------------

        $fields = array();
        $fields['pid_profile_addby'] = SPI::I('Iam')->get_id() ? SPI::I('Iam')->get_id() : null;
        $fields['gid'] = $gid;
        $fields['pid'] = $pid == 0 ? null : $pid;
        $fields['order_index'] = $max_order_index;
        $fields['description'] = $description ? $description : null;
		$fields['t_created'] = strftime('%Y-%m-%d %H:%M:%S');
		
        $fields['extension']    = isset( $media_attrs['extension'] )    ? $media_attrs['extension']     : null;
        $fields['width']        = isset( $media_attrs['width'] )        ? $media_attrs['width']         : null;
        $fields['height']       = isset( $media_attrs['height'] )       ? $media_attrs['height']        : null;

        $result = SPIstore::I()->insert(
            SPIcfg::$store['table:media'],
            $fields,
            null,
            array( 'id' => SPIcfg::$store['sequence:global'] )
        );

        return $result === false ? false : $result['id'];
    }

    public function modify( $item_id, $description, $media_attrs, $order_index )
    {
        $current_item = $this->get_by_id( $item_id );

        if( $current_item === false )
        {
            return false;
        }
        SPIm::I('media')->clean_cache( $current_item );

        // --------------------------------------------------------------------

        $fields = array();
        $fields['description']  = $description ? $description : null;

        if( isset( $media_attrs['extension'] ) )        $fields['extension']    = $media_attrs['extension'];
        if( isset( $media_attrs['width'] ) )            $fields['width']        = $media_attrs['width'];
        if( isset( $media_attrs['height'] ) )           $fields['height']       = $media_attrs['height'];
        if( isset( $order_index ) )                     $fields['order_index']  = $order_index;

        return SPIstore::I()->modify(
            SPIcfg::$store['table:media'],
            $fields,
            array( 'id' => $item_id )
        );
    }

    public function delete( $item_id )
    {
        $current_item = $this->get_by_id( $item_id );

        if( $current_item === false )
        {
            return false;
        }
        SPIm::I('media')->clean_cache( $current_item );

        // --------------------------------------------------------------------

        $media_source = SPIm::I('media')->get_filesystem_source( $item_id );

        if( file_exists( $media_source ) )
        {
            unlink( $media_source );
        }

        // --------------------------------------------------------------------

        return SPIstore::I()->delete(
            SPIcfg::$store['table:media'],
            array( 'id' => $item_id )
        );
    }

    public function delete_group( $gid, $pid )
    {
        $qresult = $this->select( $gid, $pid );

        if( $qresult === false )
        {
            return false;
        }

        while( $item = $qresult->fetch() )
        {
            $this->delete( $item['id'] );
        }
        $qresult->close();

        return true;
    }

    public function reassign_media_pack( $source_gid, $source_pid, $new_gid, $new_pid )
    {
        return SPIstore::I()->modify(
            SPIcfg::$store['table:media'],
            array( 'gid' => $new_gid, 'pid' => $new_pid ),
            array( 'gid' => $source_gid, 'pid' => $source_pid )
        );
    }


    public function load_file( $item_id, $tmp_uploaded_source )
    {
        $media_source = SPIm::I('media')->get_filesystem_source( $item_id, true );

        if( file_exists( $media_source ) )
        {
            unlink( $media_source );
        }

        if( move_uploaded_file( $tmp_uploaded_source, $media_source ) === false )
        {
            $this->delete( $item_id );
            return false;
        }

        return true;
    }
}
